/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"

/** 
  * @brief  DefineSTENUM
  */ 
static uint8_t hex_to_str_buffer[16*2+1];  // 12:45:78:01:34:67\0
/**
  * @}
  */


/**
  * @brief  char_for_nibble.
  * @param  None
  * @retval None
  */
uint8_t char_for_nibble(uint8_t nibble)
{
    if (nibble < 10) return '0' + nibble;
    nibble -= 10;
    if (nibble < 6) return 'A' + nibble;
    return '?';
}



/**
  * @brief  hex_to_str.
  * @param  None
  * @retval None
  */
uint8_t *hex_to_str(uint8_t* addr, uint8_t len)
{
    // orig code
    // sprintf(hex_to_str_buffer, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
    // sprintf-free code
	uint8_t i = 0;
    uint8_t *p = hex_to_str_buffer;
    for (i = 0; i < len ; i++) {
        *p++ = char_for_nibble((addr[i] >> 4) & 0x0F);
        *p++ = char_for_nibble((addr[i] >> 0) & 0x0F);
    }
    *p = 0;
    return (uint8_t *) hex_to_str_buffer;
}


/**
  * @brief  char2hex.
  * @param  None
  * @retval None
  */
int8_t char2hex(uint8_t s) 
{
	return ( (s>='0' && s<='9') ? (s-'0') : ((s>='a' && s<='f') ? (s-'a'+0xa) : (((s>= 'A' && s<='F') ? (s-'A'+0xa) : 0xFF))) );
}

/**
  * @brief  str_to_addr.
  * @param  None
  * @retval None
  */
int8_t str_to_addr(uint8_t* dest, uint8_t *p) 
{
	int i;
	int32_t high_4bits;
	int32_t low_4bits;
	for( i = 0 ; i < 6 ; i++)
	{
		high_4bits = char2hex(p[2*i]);
		low_4bits = char2hex(p[2*i+1]);
		if (high_4bits == 0xffff || low_4bits == 0xffff) {
			return -1;
		}
		dest[i] = high_4bits << 4 | low_4bits;
	}
	return 0;
}

/**
  * @brief  cmdGeCommaParameter.
  * @param  None
  * @retval None
  */
void cmdGeCommaParameter(uint8_t *s, uint16_t len, uint8_t *retparam0, uint32_t *retparam0LEN, uint8_t *retparam1, uint32_t *retparam1LEN)
{
	//Param, SubParam1
	uint8_t *ptr = &s[0];
    uint8_t CountNum = 0;
    for(int index = 0; index < len; index ++)
    {
        CountNum ++;
        if(*++ptr == ',')
        {
            break;
        }
    }
    ptr ++;
    memcpy(retparam0, s, CountNum);
    memcpy(retparam1, ptr, (len - CountNum));
    retparam0[CountNum] = '\0';
    retparam1[(len - CountNum)] = '\0';
    *retparam0LEN = CountNum;
    *retparam1LEN = (len - CountNum);
}


/**
  * @brief  str2hex_value字符串转成16进制数
  * @param  None
  * @retval None
  */
uint32_t str2hex_value(uint8_t *s, uint32_t len)
{
	//Param, SubParam1
    uint8_t HexArr[32];
    uint32_t RetHexValue = 0;
    str_to_hex_arr(s, HexArr, len/2);
    /* 16进制数组转成16进制的值需要按照高低位顺序移位操作
    * 具体操作按照如下规则
        --0xd38888--
        {0xd3, 0x88, 0x88}
        0xd3 << 2*8
        0x88 << 1*8
        0x88 << 0*8 
    **/
    for(int index = 0; index < len/2; index ++)
        RetHexValue = RetHexValue + (HexArr[index] << (8*(len/2 - (index + 1))));
    return RetHexValue;
}


/**
  * @brief  str2hexArr字符串转成16进制数组
  * @param  None
  * @retval None
  */
void str2hexArr(uint8_t *s, uint32_t len, uint8_t *desArry)
{
	//Param, SubParam1
	int32_t high_4bits;
	int32_t low_4bits;
	for(int index = 0 ; index < len/2 ; index ++)
	{
		high_4bits = char2hex(s[2*index]);
		low_4bits = char2hex(s[2*index+1]);
		desArry[index] = high_4bits << 4 | low_4bits;
	}
}

/**
  * @brief  cmdGetSubParameter.
  * @param  None
  * @retval None
  */
int cmdGetSubParameter(uint8_t *s, uint16_t len, uint16_t offset, uint8_t *retparam)
{
	//AT+CMD=Param,SubParam1,SubParam2,SubParam3,....
	uint8_t *ptr = &s[0];ptr = ptr + (len - offset);
	if(offset == 0)//AT+XXX
	{
		return 0;
	}
	else if(offset > 1)//AT+XXX=......
	{
		if(*ptr == '=')
		{
			ptr ++;
			memcpy(retparam, ptr, (offset - 1));
			return (offset - 1);
		}
		else//AT+XXX......错误
		{
			return -1;
		}
	}
	return 0xFF;
}

/**
  * @brief  MyAtoi
  * @param  None
  * @note	xxx
  * @retval None
  */
int MyAtoi(const char *str)
{
	int s=0;
	bool flag=false;
	
	while(*str==' ')
	{
		str++;
	}
 
	if(*str=='-'||*str=='+')
	{
		if(*str=='-')
		flag=true;
		str++;
	}
 
	while(*str>='0'&&*str<='9')
	{
		s=s*10+*str-'0';
		str++;
		if(s<0)
		{
			s=2147483647;
			break;
		}
	}
	return s*(flag?-1:1);
}


/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
